/*
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 */
function onAddNewCategory(frmID, msgDiv, cattable, cbo) {
	var newId = '';
	var linkto = '';
	
	$(frmID).request( {
			asynchronous: false,
			parameters: $(frmID).serialize(true),
	  		onComplete: function(transport) { 
			  	var response = transport.responseText || "";
		    	
		    	$(msgDiv).innerHTML = '';
		    	//alert(response);
		    	if(response != "") {
		    		obj = response.evalJSON();
		    		
		    		
		    		if(obj == null) {
		    			$(msgDiv).update('Something wrong! Reponsed string is: ' + response);
		    		}
		    		
			    	if(msgDiv != null)
				    	$(msgDiv).update(obj.message);
				    if(obj.linkactive!=null)
						linkto = obj.linkactive;
		    	}
	  		}
		});
		$(msgDiv).style.display = 'block';
		Fat.fade_element(msgDiv, null, 1500, null, null);
		if(newId != '') {
			Fat.fade_element(newId);
			$(frmID).reset();
		}
		//revent submit page
		return false;
	//}
}

function onDeleteCat(url, catId, cbo,catparent,cat_position) {
	var isOK = -1;
	
	if(!confirm('Are you sure you want to delete the category with Id:' + catId)) {
		return false;
	}
	
	new Ajax.Request(url, {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=delcat&id=' + catId+'&catparent='+catparent+'&cat_position='+cat_position,
	  onSuccess: function(transport) {
			var response = transport.responseText.trim();
			
			//alert(response);
			
			obj = response.evalJSON();

    		if(obj == null){
    			$('ajaxResponse1').innerHTML = 'Something wrong! Reponsed string is: ' + response;
    		}
    		
    		isOK = obj.code;
		
    		$('ajaxResponse1').update(obj.message);
		}
	});

	$('ajaxResponse1').style.display = 'inherit';
	Fat.fade_element('ajaxResponse1', null, 1500, null, null);

	if(isOK == 0) {
		
		rowID = 'cat-' + catId;
		Fat.fade_element(rowID, null, 1000, "#f22", null);
		setTimeout("$(rowID).remove()", 1000);
		
	}				
}


function onEditCat(frmID, msgDiv) {
	var newId = '';
	var status = null;
	var ret = null;
	var message = null;
	var linkto = '';
	$(frmID).request( {
			asynchronous: false,
			parameters: $(frmID).serialize(true),
	  		onComplete: function(transport) { 
			  	var response = transport.responseText || "";
				//alert(response);
		    	$(msgDiv).innerHTML = '';
		    	if(response != "") {
		    		obj = response.evalJSON();
		    		if(obj == null) {
		    			$(msgDiv).update('Something wrong! Reponsed string is: ' + response);
		    		}
		    		
			    	if(msgDiv != null)
				    	$(msgDiv).update(obj.message);
					if(obj.cat_id==0) {
						ret =1;	
						message = obj.msg;
					}
					if(obj.linkactive!=null)
						linkto = obj.linkactive;
					
		    	}
	  		}
		});
	if(linkto!="") {
		document.location = linkto;
	} else {
	$(msgDiv).style.display = 'block';
	Fat.fade_element(msgDiv, null, 1500, null, null);
	//revent submit page
	}
	return false;
}

function onSetActive(url, catId,catname, checkid) {
	var txt='';	
	if(checkid.checked != true)
		txt = 'in';
	if(!confirm('Are you sure you want to '+txt+'active menu '+catname)) {
		if(checkid.checked == true)
			checkid.checked =false;
		else
			checkid.checked =true;
		return false;
	}
	$value = 0;	
	if(checkid.checked == true)
		$value = 1;
	new Ajax.Request(url, {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=setactive&id='+catId+'&value=' + $value,
	  onSuccess: function(transport) {			
	 }
	});
		
}

function onSetDefault(url, catId,catname, checkid,default_id) {
	default_radio = document.getElementById(default_id)
	if(!confirm('Are you sure you want to set default menu '+catname)) {
		if(checkid.checked == true) {
			checkid.checked =false;
			default_radio.checked = true;
		}
		else
			checkid.checked =true;
		return false;
	}
	$value = 0;	
	if(checkid.checked == true)
		$value = 1;
	new Ajax.Request(url, {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=setdefault&id='+catId+'&value=' + $value,
	  onSuccess: function(transport) {			
	  }
	});
		
}

function onLimitCat(url,page) {
	limit = "";
	new Ajax.Request(url, {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=setlimit',
	  onSuccess: function(transport) {	
	  	var response = transport.responseText.trim();
			
			obj = response.evalJSON();
			if(obj.limit!="")
	 			limit = obj.limit;
	 }
	});
	if(limit!="")
			alert('Number of nested top category is exceeded. Maximum is '+limit);
	else
		document.location = page;
}